package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.User;
import cn.kgc.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/7/20
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    /*

       传统方式：
            localhost:8080/user/addUser
            localhost:8080/user/selectUser
            localhost:8080/user/deleteUser?id=1
            localhost:8080/user/updateUser

        restful 风格设计请求格式
            localhost:8080/user      查询所有   get
            localhost:8080/user/1    根据id查询   get
            localhost:8080/user      添加用户   post
            localhost:8080/user/1    删除      delete
            localhost:8080/user     修改   put
     */

    @GetMapping
    public Object getAll(){
        List<User> all = userService.getAll();
        return all;
    }

    //    /user/1
    @GetMapping("{id}")
    public Object getOne(@PathVariable Integer id){

        User user = userService.getOne(id);
        return user;
    }


    @PostMapping
    public User addOne(@RequestBody User user){
        User usr =  userService.addOne(user);
        return usr;
    }


    @DeleteMapping("{id}")
    public Object deleteOne(@PathVariable Integer id){
        Boolean b =   userService.deleteOne(id);
        return b;
    }


    @PutMapping
    public User updateOne(@RequestBody User user){

        return null;
    }


}
